package com.jit.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jit.app.frament.GameFragment;
import com.jit.app.frament.HomeFragment;
import com.jit.app.frament.ManagerFragment;
import com.jit.app.frament.SoftFragment;

import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    RadioGroup tabs;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setAndroidNativeLightStatusBar(this, true);
        initView();
    }

    public void initView(){
        fragmentManager = this.getSupportFragmentManager();
        tabs = findViewById(R.id.tab_container);
        tabs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.tab_home:
                        checkFragment(HomeFragment.class, HomeFragment.tag);
                        break;
                    case R.id.tab_game:
                        checkFragment(GameFragment.class, GameFragment.tag);
                        break;
                    case R.id.tab_soft:
                        checkFragment(SoftFragment.class, SoftFragment.tag);
                        break;
                    case R.id.tab_manager:
                        checkFragment(ManagerFragment.class, ManagerFragment.tag);
                        break;
                }
            }
        });

        checkFragment(HomeFragment.class, HomeFragment.tag);//初始选中home
    }

    public Class<? extends Fragment> currentFragment;//当前选中的fragment
    public Map<Class<? extends Fragment>, Fragment> fragmentsMap = new HashMap<>();//保存所有Fragment

    //切换Fragment
    private void checkFragment(Class<? extends Fragment> fragmentClass, String tag){
        Fragment fragment = fragmentsMap.get(fragmentClass);
        //如果还没有创建这Fragment，就创建并添加
        if(fragment == null){
            try{
                fragment = fragmentClass.newInstance();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.fragment_container,fragment,tag);
                fragmentTransaction.commit();
                fragmentsMap.put(fragmentClass, fragment);
            }catch(InstantiationException|InvalidParameterException|IllegalAccessException e){
            }
        }

        //把之前的fragment隐藏
        Fragment f = fragmentsMap.get(currentFragment);
        if(f != null){
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.hide(f);
            fragmentTransaction.commit();
        }
        //把当前Fragment显示出来
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();


        currentFragment = fragmentClass;

    }


    public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }
}
